/*
 * Copyright 2011 OpenAjax Alliance
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* ---------------------------------------------------------------- */
/*                  Initialize namespace properties                 */
/* ---------------------------------------------------------------- */

/**
 * @namespace OAA_CI.controls
 */

OAA_CI.controls = OAA_CI.controls || {};

OAA_CI.controls.allControlsView = new OAA_CI.SidebarView('ID_TREECHILDREN_ALL_CONTROLS');
OAA_CI.controls.labelsView      = new OAA_CI.SidebarView('ID_TREECHILDREN_LABELS');
OAA_CI.controls.controlsView    = new OAA_CI.SidebarView('ID_TREECHILDREN_CONTROLS');

OAA_CI.controls.views = [
  OAA_CI.controls.allControlsView,
  OAA_CI.controls.labelsView,
  OAA_CI.controls.controlsView
];

OAA_CI.controls.hideMeter = function () { OAA_CI.controls.meter.hidden = true; };
OAA_CI.controls.showMeter = function () { OAA_CI.controls.meter.hidden = false; };

OAA_CI.sidebar = OAA_CI.controls;

/**
 * getDisplayProperties
 *
 * @desc Add getDisplayProperties method to cache elements;
 *       called by populateTreeAs... functions.
 *
 * @return List of property values to be displayed in the sidebar
 */

OpenAjax.a11y.cache.FormElement.prototype.getDisplayProperties = function () {
  return [this.toString(), "", "", this.dom_element.getAccessibility()];
};

OpenAjax.a11y.cache.FieldsetElement.prototype.getDisplayProperties = function () {
  return [this.toString(), "", "", this.dom_element.getAccessibility()];
};

OpenAjax.a11y.cache.LegendElement.prototype.getDisplayProperties = function () {
  return [this.dom_element.tag_name, this.label, "", this.dom_element.getAccessibility()];
};

OpenAjax.a11y.cache.LabelElement.prototype.getDisplayProperties = function () {
  return [this.dom_element.tag_name, this.label, "", this.dom_element.getAccessibility()];
};

OpenAjax.a11y.cache.InputElement.prototype.getDisplayProperties = function () {
  return [this.toString(), this.getLabelNLS(), this.dom_element.getHasDescribedBy(), this.dom_element.getAccessibility()];
};

OpenAjax.a11y.cache.ButtonElement.prototype.getDisplayProperties = function () {
  return [this.dom_element.tag_name, this.getLabelNLS(), this.dom_element.getHasDescribedBy(), this.dom_element.getAccessibility()];
};

OpenAjax.a11y.cache.TextareaElement.prototype.getDisplayProperties = function () {
  return [this.dom_element.tag_name, this.getLabelNLS(), this.dom_element.getHasDescribedBy(), this.dom_element.getAccessibility()];
};

OpenAjax.a11y.cache.SelectElement.prototype.getDisplayProperties = function () {
  return [this.toString(), this.getLabelNLS(), this.dom_element.getHasDescribedBy(), this.dom_element.getAccessibility()];
};

OpenAjax.a11y.cache.OptgroupElement.prototype.getDisplayProperties = function () {
  return [this.dom_element.tag_name, this.label, "", this.dom_element.getAccessibility()];
};

OpenAjax.a11y.cache.OptionElement.prototype.getDisplayProperties = function () {
  return [this.dom_element.tag_name, this.label, "", this.dom_element.getAccessibility()];
};

OpenAjax.a11y.cache.WidgetElement.prototype.getDisplayProperties = function () {
  return [this.toString(), this.label, this.dom_element.getHasDescribedBy(), this.dom_element.getAccessibility()];
};

/**
 * getChildren
 *
 * @desc Add getChildren method to cache elements; called by
 *       populateTreeAsHierarchy function.
 *
 * @return List of child elements
 */

OpenAjax.a11y.cache.FormElement.prototype.getChildren = function () {
  return this.child_cache_elements;
};

OpenAjax.a11y.cache.FieldsetElement.prototype.getChildren = function () {
  return this.child_cache_elements;
};

OpenAjax.a11y.cache.LegendElement.prototype.getChildren = function () {
  return this.child_cache_elements;
};

OpenAjax.a11y.cache.LabelElement.prototype.getChildren = function () {
  return this.child_cache_elements;
};

OpenAjax.a11y.cache.InputElement.prototype.getChildren = function () {
  return [];
};

OpenAjax.a11y.cache.ButtonElement.prototype.getChildren = function () {
  return this.child_cache_elements;
};

OpenAjax.a11y.cache.TextareaElement.prototype.getChildren = function () {
  return [];
};

OpenAjax.a11y.cache.SelectElement.prototype.getChildren = function () {
  return this.child_cache_elements;
};

OpenAjax.a11y.cache.OptgroupElement.prototype.getChildren = function () {
  return this.child_cache_elements;
};

OpenAjax.a11y.cache.OptionElement.prototype.getChildren = function () {
  return [];
};

OpenAjax.a11y.cache.WidgetElement.prototype.getChildren = function () {
  return this.child_cache_elements;
};


/**
 * @function onLoad
 *
 * @memberOf OAA_CI.controls
 *
 * @desc Initialize sidebar and populate its default tabpanel.
 */

OAA_CI.controls.onLoad = function () {

  var config = {
   rules      : true,
   attributes : true,
   properties : true,
   style      : false,
   events     : true
  };

  var pt = new OAA_CI.Properties.PropertiesTabPanel("ID_PROPERTIES", config);
  OAA_CI.controls.allControlsView.addProperties(pt);  
  OAA_CI.controls.labelsView.addProperties(pt);
  OAA_CI.controls.controlsView.addProperties(pt);

  OAA_CI.controls_nls_strings = document.getElementById("ID_STRINGBUNDLE_CONTROLS");
  OAA_CI.controls.meter = document.getElementById('ID_METER_CONTROLS');

  OAA_CI.controls.allControlsView.isInitialized = false;
  OAA_CI.controls.allControlsView.showCacheItems();

};

/**
 * @function allControlsView.showCacheItems
 *
 * @memberOf OAA_CI.controls
 *
 * @desc Respond to the Tree View button in the sidebar's main tab panel.
 *       If the panel has not been initialized, populate it with control
 *       elements structured as a hierarchical list. Update properties
 *       display and page highlighting based on selection state of panel.
 */

OAA_CI.controls.allControlsView.showCacheItems = function () {
  var view = OAA_CI.controls.allControlsView;

  if (!view.isInitialized) {
    // Save references
    view.tab   = document.getElementById('ID_TAB_ALL_CONTROLS');
    view.tree  = document.getElementById('ID_TREE_ALL_CONTROLS');
    view.cache = OAA_CI.ruleset.dom_cache.controls_cache;

    // Populate the tabpanel
    var control_elements = view.cache.child_cache_elements;
    OAA_CI.populateTreeAsHierarchy(control_elements, 'ID_TREECHILDREN_ALL_CONTROLS');
    view.isInitialized = true;
  }

  view.updateItemSelectionDisplay();
};

/**
 * @function labelsView.showCacheItems
 *
 * @memberOf OAA_CI.controls
 *
 * @desc Respond to the Labels button in the sidebar's main tab panel.
 *       If the panel has not been initialized, populate it with label
 *       elements structured as a flat list. Update properties display
 *       and page highlighting based on selection state of panel.
 */

OAA_CI.controls.labelsView.showCacheItems = function () {
  var view = OAA_CI.controls.labelsView;

  if (!view.isInitialized) {
    // Save references
    view.tab   = document.getElementById('ID_TAB_LABELS');
    view.tree  = document.getElementById('ID_TREE_LABELS');
    view.cache = OAA_CI.ruleset.dom_cache.controls_cache;

    // Populate the tabpanel
    var label_elements = view.cache.label_elements;
    OAA_CI.populateTreeAsFlatList(label_elements, 'ID_TREECHILDREN_LABELS');
    view.isInitialized = true;
  }

  view.updateItemSelectionDisplay();
};

/**
 * @function controlsView.showCacheItems
 *
 * @memberOf OAA_CI.controls
 *
 * @desc Respond to the Controls button in the sidebar's main tab panel.
 *       If the panel has not been initialized, populate it with control
 *       elements structured as a flat list. Update properties display
 *       and page highlighting based on selection state of panel.
 */

OAA_CI.controls.controlsView.showCacheItems = function () {
  var view = OAA_CI.controls.controlsView;

  if (!view.isInitialized) {
    // Save references
    view.tab   = document.getElementById('ID_TAB_CONTROLS');
    view.tree  = document.getElementById('ID_TREE_CONTROLS');
    view.cache = OAA_CI.ruleset.dom_cache.controls_cache;

    // Populate the tabpanel
    var control_elements = view.cache.control_elements;
    OAA_CI.populateTreeAsFlatList(control_elements, 'ID_TREECHILDREN_CONTROLS');
    view.isInitialized = true;
  }

  view.updateItemSelectionDisplay();
};

/**
 * @function onItemSelectionChange
 *
 * @memberOf OAA_CI.controls
 *
 * @desc Handle the onselect event from the XUL tree in the main tab panel
 *       by calling the updateItemSelectionDisplay method.
 *
 * @param {Number} index - Specifies the view that triggered the onselect event.
 */

OAA_CI.controls.onItemSelectionChange = function (index) {

  var view = OAA_CI.controls.views[index];
  view.updateItemSelectionDisplay();

};
